//
//  ContactTabelViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14/10/31.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ContactTabelViewController.h"
#import "Contact.h"
#import "ContactDetailViewController.h"
#import "ContactCell.h"
@interface ContactTabelViewController ()
@property(nonatomic, strong)NSArray *contacts;
@property(nonatomic, strong)NSString *content;
@end

@implementation ContactTabelViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initContacts:(NSArray *)contacts
        withContent:(NSString *)content{
    _contacts = contacts;
    _content = content;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = _content;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _contacts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    static NSString *CellIdentifier = @"ContactCell";
    Contact *contact = _contacts[indexPath.row];
    
    ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ContactCell *contactCell = (ContactCell *)cell;
    [contactCell initWithContact:contact];
    
    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
    ContactDetailViewController *detailViewController = segue.destinationViewController;
    Contact *contact = _contacts[selected.row];
    [detailViewController initWithContact:contact];
}
@end
